small niggle: `else` on the same line as `}` and space after `if` before `(`, same for a few others in here.