Various simplifications

Localized everything- no more globals.
Simplified try/finally block