Fixes #8

Also some code cleanup.