added prefetch. reorganized code