Continued rewriting. Cleaning up messy code.