Lots of changes suggested by Coder and PHP_CodeSniffer