Make code easier to read and more consistent and fix a few bugs