This function is quite slow, and appears to be the main bottleneck at the moment. **Suggestion:** Read chunks of data, instead of reading values one by one?