Added unit tests, refactored code, plus some minor changes