opimization of code to speed up