Working on vectorizing the code properly.