Cleanup, refactoring, some work on issue #2