a few bug fixes and code cleanup