bug fixes and refactorings