refactoring code and begining to add tests