First working solution
Switched to a while loop instead of #each_with_index