bug fixes, refactoring