sped up, need to work on optimization routine