Extract the script task handlers code into separate methods

This is a fix for bug #3811 , thank you.