restructured some logic in _extract_scripts