bug fixes and cleanup all over the place