Refactor channel code

cc @malcyl, @ccare