One way to handle arbitrary device nesting without hardcoding a large number of levels is to create a recursive device handler subroutine. The main loop calls the device handler for each device. In addition to processing the device's channels, the device handler recursively calls itself for each nested device.