Branch support

@jamescostian's master, with my fixes and tests.

Supersedes #17.