fixed miscalculated array indices