Fix crashes in regen_metadata

This PR _should_ solve issue #118.