Code cleanup

A couple of fixes and some organization (untested but should work)