Refactoring; adding some unit tests