re-factored patch to make it more testable, added unit test