Updating script to be more robust