My mistake this is a simplified version of the code I made quickly to post here (i.e I deleted a lot of the manipulation of the data that happens after the array as this is not relevant) I have changed the code now. Should have given it a quick proof read before posting, my bad.