added optimisations in the run function