refactored hdf5 back by splitting large code into smaller functions.