Write a proper IO loop