Fixes #206, cleanup, refactoring
Fixes #270