Refactored some code I had trouble reading.