Improve algorithm of loading necessary scripts