Lots of style improvements, overall optimization

The last version of this file was admittedly terrible.