Refactor callback function