Refactored the async script loading code