minor bug fixes and refactorings