Add bugfixes to save function