Some minor fixes + refactoring