wrapped code for optimization and opitimized a single run