bug fixes and cleanup