a more functional approach and a little speedier