Refactor and generalize some post-process functions