Data is disturbed by his first nightmare, in which a group of workmen attack and destroy him. He tells Geordi about the dream while the pair work to install the ship's new warp core, and Geordi points out that he has probably reached a new level in his dream program. Soon, Picard and Riker interrupt to try out the new warp core. But when they attempt to engage the drive, it fails, and the lights on the Enterprise shut off. Disappointed, Geordi says that he will have to reconfigure the plasma conduit. That evening, Troi pays a visit to Data in his quarters, having been alerted to his nightmare. Data tells her that he is afraid to activate his dream sequence again, but she encourages him to keep at it. Data is soon asleep and dreaming, this time seeing Worf eating a piece of "cellular peptide cake," which is really Troi, who has taken the form of a half-woman, half-cake. He awakens surrounded by a concerned Troi, Worf, and Geordi, who inform him that his chronometer failed and he has overslept for the first time.
Data meets with Troi again and tells her that, although he has run three complete self-diagnostics, he has found no explanation for his nightmares. Troi suggests to Data he could actually be developing a neurosis. Data follows Troi into the Turbolift, where he inexplicably stabs her in the shoulder with a coil brace.
Seconds later, Riker and Worf enter the Turbolift to find Data and the bleeding, unconscious Troi. Data immediately attacks Riker, but suddenly realizes what he is doing and stops. Later, he tells Picard and the crew that he had an uncontrollable urge to eliminate the image of a mouth that he saw on Troi's shoulder — an image first seen in his dream program. Since the dream program has been turned off, no one understands why these images are still appearing, so Geordi suggests taking time to conduct a sub-polymer scan of Data. In the meantime, Picard orders him confined to quarters. Meanwhile, in Sickbay, Beverly discovers a strange interphasic rash on Troi's shoulder near her stab wound. She brings Picard in to see the rash, which is quickly getting worse, and upon examining Troi with an interphasic scanner, realizes that she — and most of the rest of the crew — are covered with leech-like creatures.
Soon afterward, Beverly grimly reports that the creatures are extracting their cellular peptides and will eventually destroy them if they are not stopped. Unfortunately, stopping them is impossible since the organisms exist only in an interphasic state beyond normal vision or sensor acuity. Everyone is at a loss, until Picard realizes that the images Data has been seeing in his nightmares, like the "mouth" on Troi's shoulder, correspond with the actual locations of the creatures. He decides to link Data's neural net to the Holodeck so they can observe his dream images. Geordi hooks Data up and Data activates his dream program, and Picard and Geordi then enter his dream. They see the "Troi cake," meet Sigmund Freud and encounter the workmen, who reveal the warp plasma conduit. The workmen attack Picard and Geordi, and Data makes a shrieking noise that causes them pain. The dream somehow makes sense to Data, and he wakes up and he Instructs Geordi to adjust his positronic subprocessor to emit an interphasic pulse, theorizing that the high frequency pulse will eliminate the creatures. The plan successfully drives away the creatures, who were also disrupting the warp drive, leaving Data with an increased appreciation for his "human" gifts.