almost done refactoring ecb cut and paste
particularly tricky piece of logic