few bug fixes, code cleanup