Updated build script to handle exit code