lots of great cleanup and bug fixes