Added new tests, refactored code