Fixing unit tests and slight code refactoring