Wrote more comprehensive tests, and refactored duplicated code