Tried to optimize loop