Added a vector version of the code. This gave the highest performance improvement.