bug fixes and code refactorings