Major refactoring, API change

- Rewrote most of the stack
- Fixes #156