More refactoring, cleanup. Now works very basically.