Made scripts more robust to failure. Fixed bash/sh bug.