Refactoring and some tests written