removed i_max and j_max (refactor)