Inlined some variables, cleaned up some code and removed obvious bugs.