Update publish script and make it more robust