refactoring and adding some tests