reorder if-else tree, factor out max turns magic number.  Normalise rest of indentation to tabs.