First refactor for #1571