Refactored a lot of code, started adding unit tests