Minor bug fixing & refactoring