Revert "major refactor" for reapplication of PRs

The refactor removed a bunch of PRs (like gh-188). Rolling back the last known good state so @deanmao can PR the refactor from his fork without wiping out previous changes.