Slight refactor, and added test coverage.