Attempted to clean up a bit in this messy, messy code