Refactored instrument main loop into separate functions