Minor modifications. Added utility function indexOfMax.