moved algorithm to to top of file