Lots of fixes and cleanup.