more attempted bug fixes
a lot of cleaning up code so others can really read it