latest code with save function matched to input