changed code to hopefully resolve array overflow errors