Refactorings and cleanups

None of the changes should affect program logic or functionality, they're in preparation for another pull request