Functionnalize code and add tests