Refactoring + adding unit tests