Some minor refactoring and code improvements