bug fixes and clean up to refactor