Fix various script bugs