refactoring and fixed tests