Started refactoring, added tests also