Cleanup

Very basic cleanup. A lot more could be done, especially with unused globals.