Minor refactoring, some optimizing