Refactor to clean up flags, types, and fix bug.

The old code was not handling the channels properly, which effectively
doubled the frequency.