Refactoring code to make it more modular; Start unittests