Refactored redundant logic into utility methods, deleted some dead code

I put some duplicated code either into local helper functions or into `Utils`, depending on how generic it was.