bug fixes, code cleanup