added save function and also piped the construction of the final data