minor bug fixes, refactoring