Cleanup and refactor in publish.py.