Fix #7, and clean up a some messy code.