Cleaned up the code and made it a tad DRYer

Nothing major, but there's a few parts that seem to repeat.