Code rewriting; cleaner, better documented/organized and hopefully more flexible.