Updated to read data from a callback rather than from stdio as this is more realistic to most use cases.