Fixed tests and refactoring