Fixed bug in tests and performed some refactoring