Started rewrite. Fixes #1