Group origin shifts after regrouping

Hi all! A question from a novice dynamo-coder.

I wrote a script for making changes to groups (using the mechanism “ungroup - make changes - regroup - replace identical instances of a group”).

Everything works as it should, but then I found an annoying problem - for some groups (e.g. containing stairs), after the regrouping, the group origin is shifted. As a result, when replacing old groups with new ones, new groups are inserted incorrect with an offset.

Before using the script:

After:

Tried different solutions (including clockwork’s Element.Location), but unsuccessfully. Who faced this? How can this problem be solved? I will be glad to any idea.

In applications - the actual script (dyn) and rvt-file (ver.2021) for demonstration. Thanks.

groups_test.rvt (2.0 MB)
groups_test.dyn (8.7 KB)