Please extract this code into a standalone function.