minor bug fixes and code cleanup