Updated algorithm to use two seperate indexing variables for temp and regular buffer