Refactor pre and post command code into functions