various refactorings and refinements