Started to refactor and add unit tests