multiple bug fixes & refactoring