fix and clean the code + add tests