Refactored code that I wrote when I didn't know Python well enough.