Merged everything; added unit tests and did extensive refactoring