added timing code from stackoverflow