Updated tests and some refactoring