handle IO in a functional way