more bug fixes and cleanup