Saner code formatting, fix a couple of bugs