optimization of code to speed up