Decomposed a few walls of code into better-structured functions