one fix for mistake in prev commit and a fair bit of tidy (untested)