Improve performance of script