Re-organizing code

Now that I've completed the prototype and convinced myself this will
work, I'm about polishing the code.