code clean up; bug fixes