Refactor code

Fixes #218