WIP refactoring for clearer logic