bugbix and minor refactoring