Added coverage, and did some code refactoring