Refactor a lot

Fixes #1