Factor out code for reading data