Refactoring of the way inputs are generated.
Minor fixes to ensure array entries exist.  Move maxima code to a single box at the end.
Typos.
Refactoring to take advantage of methods within the question.