Some refactoring for clearer code grouping