Fixed error where current array was overwritten while calculating next