Refactor code for testability, add tests for diff.py subpart changes