little optimisation in for loop, but need optimisation