Cleaned up some of the more messy code.