Refactored scripts to enable better error checking.