Refactoring, minor fixes