various bug fixes and cleanup