Further attempts at improving code readability, reduce some more duplication, and move logic to where it is appropriate