Trying to avoid circular dependencies by moving around a few functions

Tests (incl. new one) all pass.