various refactorings for clearer structure