fully vectorized functions. still works slowly.