Captain Janeway and the Doctor run into a problem as they travel back from a medical symposium on the Delta Flyer. When Janeway returns to the U.S.S. Voyager, she tells Chakotay that they encountered a race called the R'Kaal who have outlawed conventional warp travel through their territory, and insist on punishing Voyager by dismantling the ship. Janeway says she agreed to surrender their warp core in exchange for letting the crew settle on an M-Class planet. Chakotay is concerned that the captain is giving in so easily, but Janeway insists she's tired of continually risking her people on a slim chance of making it home, and orders Chakotay to set a course for the planet. Janeway then asks Torres to modify the Flyer's tractor beam so that if the warp core is ejected, it can be safely towed at warp speed. Chakotay learns of this order, and tries to find out from Janeway what's really going on. Janeway acts strangely distant, and retires to her quarters with a "headache." Chakotay visits Sickbay and calls for the Doctor, who beams in from elsewhere on the ship. Chakotay asks him to re-examine Janeway to see what the aliens may have done to her. Later, Chakotay gets a message from Supreme Archon Loth of the R'Kaal Imperium, warning that if Voyager does not surrender its warp core in ten hours, his armada will destroy the ship. Chakotay asks Seven of Nine and Harry Kim to pinpoint the source of that transmission. After finding out from the Doctor that the captain is normal, Chakotay confronts Janeway in her quarters about her decision. He makes up a story about Janeway's past that she pretends to remember, thus exposing her as an imposter. The imposter then overpowers Chakotay and knocks him out with a hypospray injection.
The imposter places the unconscious Chakotay in the ship's morgue and keeps his combadge, then returns to Sickbay and uses the mobile emitter to restore his identity - the imposter is the Doctor. The Doctor has been hearing the voices of two aliens belonging to the Hierarchy - the species that learned how to tap into the Doctor's perceptual subroutines over a year ago - and forced to do their bidding because they are holding the real Janeway hostage on their ship. On top of procuring the warp core, Zet, the leader, orders the Doctor to also acquire a series of gel packs. When Tuvok summons Chakotay, the Doctor downloads the commander's holographic template and takes on his identity. Tuvok informs him that the Flyer's com system is not damaged as the captain had reported. Meanwhile Janeway, being held behind a forcefield by the Hierarchy, tries to bluff her captors into thinking the Doctor is fooling them, but Zet refuses to abandon his plan because the warp core is worth too much money.
On Voyager, the Doctor, using Janeway's voice, summons B'Elanna Torres to the captain's quarters so he can draw her out of Engineering in order to impersonate her there. While loading up gel packs in a case, the Doctor is approached by Tom Paris, Torres' husband, for a romantic interlude. After that awkward encounter, the Doctor goes to Astrometrics as Chakotay where Seven and Kim reveal that the R'Kaal transmission originated from inside Voyager, specifically Holodeck 2. The disguised Doctor goes there with Kim, who uses residual photonic displacement to find out who was last there. He realizes the R'Kaal was actually a hologram, and the underlying template was that of the Doctor's. Exposed, the Doctor injects Kim with a hypospray, and hides him also in the morgue.
Back in Sickbay, the Doctor talks to the Hierarchy while listening to "The Blue Danube" waltz. Tuvok arrives and reveals that he's learned the Doctor downloaded the captain's holographic template while on the Flyer. When the Doctor tries to inject him with a hypospray, Tuvok is ready for the attack. The Doctor leaps through solid objects and grabs his mobile emitter to escape from Sickbay. Tuvok pursues him to a holodeck, where he's faced with a roomful of holographic copies of the Doctor. While Tuvok tries to disable the decoys, the Doctor takes a Jefferies Tube to Engineering and becomes Chakotay again, ordering an evacuation due to an imminent core breach. Paris informs Torres that he's detected the Doctor's emitter in her section, so the Doctor traps Torres behind a forcefield, activates his Emergency Command Hologram protocols and uses his command codes to eject the warp core. The Doctor then makes his way to the Delta Flyer and, using its modified tractor beam, tows the core away at warp speed.
Meanwhile Janeway helps the second Hierarchy alien, Nar, repair a salvaged component, realizing she may be able to take advantage of his friendliness. Zet puts a stop to their interaction, just as the Doctor arrives in the Delta Flyer with the warp core. Zet says he will release the captain in exchange for the core, but instead he beams the Doctor over to their ship and imprisons him with Janeway. Janeway chews out the Doctor for not following her orders to refuse cooperation, but he insists he wouldn't let her be killed.
Back on the stranded Voyager, the crew finds Chakotay and Kim in the morgue and revives them. As they try to figure out how to track down the Flyer, the ship's com system starts playing "The Blue Danube" and won't stop. But the music contains several incorrect notes, so the crew realizes it was altered intentionally. Seven analyzes the harmonics and finds a pattern that appears to be a warp signature. She scans for that signature and finds it 6.7 light-years away. Since Voyager doesn't have warp, Tuvok and Paris take a shuttle to that location.
The Hierarchy alien, meanwhile, decide to use the Doctor to infiltrate their Central Command Surveillance Complex to steal valuable information, and start uploading new data and holographic templates into his matrix. When the Doctor takes on the form of a Hierarchy alien, he starts to destabilize and has a hard time staying in one form. At that point Tuvok and Paris arrive in the shuttle to take back the Delta Flyer, and they get into a phaser fight with the Hierarchy. Zet decides to jettison Voyager's warp core and detonate it, and Nar realizes that would kill the people in the other ships. Janeway and the Doctor escape from behind the forcefield and, while the Doctor struggles with Zet, Janeway works the controls to transport the warp core back out into space, allowing Paris to reclaim it with the Flyer. Zet asks Nar to help him overpower the hologram, but Nar chooses to knock out Zet instead, putting an end to the struggle.
In danger of permanently decompiling because of the excess subroutines, the Doctor is rushed back to Voyager where Torres and Seven try to stabilize his matrix. Certain he won't survive, the Doctor starts making several "deathbed confessions" to the crew, including the revelation that he's in love with Seven. However, Torres succeeds in saving his program, ensuring a long life for the highly embarrassed Doctor.