Add support for missing scripts
Finish implementation of script error handling

Also perform code clean-up and add docstrings/comments. Fixes #1304.