change algorithm to vectorized