optimizing code loops