Some refactoring. Fixes #8