large-ish refactor