Major refactoring.

The app was in poor shape. This makes it better.