Factor out selector definition code, with tests!