working code. Waits for data to be ready before fetching it.