Complete rewrite and simplification.

Signed-off-by: Ben Corlett <bencorlett@me.com>