A lot of code cleanup. 

Mostly remove unused "Use" statements. But also fixed a few bugs, and cleaned up unused variables.