Optional arguments handling is screwed up here. The easiest way to do this is to swap the order. `handler` is required, `priority` is optional. Then most of the checks can go away.