fix compare_stage_keys and update unit tests