fix the tests, refactor the code a bit