Hello,
Honestly, I have no idea what is going on here either. Your description of the problem '..it's kind of a maze..' seems to sum it up well. My first suggestion would be to use Data::Dumper to Dump the contents of every variable you can to make SURE that what you think you are doing is correct.
Additionally, I would consider completely rewriting the script and module because it is so messy. I wish I could help you more, but a lot of important things seem to be missing from your post- such as the logic sub 'Groups', 'USERID', etc.
Lastly, I would consider creating a test file with Test::Most while you rewrite this. If you want to be even more awesome to make the code maintainable, use Moose
http://search.cpan.org/~ether/Moose-2.1204/lib/Moose/Manual.pod :)