various bug fixes and cleaning up