General refactoring, also of control flow; fixes #10