Largish refactor

 * Fixes #11 and #4