Refactor code and begin writing unit tests