Autoformatter

Fix #2540, #1041, and lack of EOF checking. Also tried to continue neatening and commenting code. I find it amusing that I am fixing a bug that should have been spotted before I was born (the EOF one).