just shuffling some code to get it closer to @jcoleman impl