bug fixes; refactoring