Code cleanup

@almell just some renaming and reorganization, to make the code look slightly less like a hack day project.