added a vectorized version of the code