Fixed logic issues, added more robust test [#58279046]