A few points: STEM manipulated Erin to put it's plan in motion, but it is not controlling Erin. If STEM had control of Erin, it would not have need of Gray to give it a body of its own, it could simply use Erin's. STEM convinces Erin to give him his own body (Grey's), but is still subject to control and oversight by Erin. It's desire to be free and out of Erin's control are extreme motivating factors for STEM to have the hacker install the root-kit.
Erin becomes suspicious of STEM following Grey and STEM's first encounter with one of the mercenaries, which could have led to STEMs discovery. He threatens Gray with shutting down and removing STEM, but STEM leverages this to its advantage by making the situation dire enough that Grey is forced to approach the hacker and have the root-kit installed out of desperation.
STEMS priority then shifts to eliminating loose ends (the mercs, the officer, and Erin) and gaining sole control over Grey's body by "breaking his mind", leading to the dramatic finale and final reveal.