all for loops optimized

scales not linear but close to linear