Writing save function