Refactor and fix lots of things

Closes #163