switched to iteration for k > MAX