Refactor code for printf callback