bug fixes + refactoring