refactor publish function