Fix code

Various changes to formatting and other aspects. Review commit by commit to take account for various changes.