The Shakespeare Code ( TV story ) The Shakespeare Code was the second episode of the third series of " Doctor Who " . It included the first televised appearance of William Shakespeare since a cameo in 1965 ' s " The Chase " . Writer Gareth Roberts was well known as a fan of Shakespeare ; he had included him as a character in " A Groatsworth of Wit " , a Ninth Doctor comic strip . The Carrionite s were also derived from the Bard ' s work , specifically the witches in " Macbeth " . " Code " was one of the most costly stories ever produced , with large expenditures on costumes and sets . Some of the expense was quickly rationalised by BBC Wales , however . The interior of the witches ' house was almost immediately reused as Sarah Jane ' s attic at 13 Bannerman Road , where its expense was amortised over the five - year run of " The Sarah Jane Adventures " . ( DWMSE 23 ) It also introduced the narrative lynchpin of Queen Elizabeth I having a severe distrust towards the Tenth Doctor . The reasoning behind why she disliked him so much wasn ' t explained until the 50th anniversary special " The Day of the Doctor " . Synopsis . As a reward for her help in the last episode , Martha Jones gets a trip in the TARDIS . The Tenth Doctor takes her to 1599 England . After viewing a performance of Shakespeare ' s latest play , the time travel lers are beset by apparent sorcery . Under threat of annihilation from a species from the Dark Times , the TARDIS team have to establish whether there is a connection between a witch they ' ve met and Shakespeare ' s " Love ' s Labour ' s Won " — a play that was legendarily lost to time . Plot . A nymph , Lilith , is serenaded from her balcony by a lute - playing swain , Wiggins . Seeing how much he desires her , Lilith allows him entrance to her home . However , upon entering home , he is shocked to find it full of horrifying witching artifacts — not what he would expect a beautiful girl to have in her home . Lilith kisses Wiggins , but on pulling away , he finds her transformed into a wrinkled hag . She decides to introduce her suitor to her two " mothers " , Mother Doomfinger and Mother Bloodtide . Much to Wiggins ' horror , the two mothers cackle and pounce on him , apparently devouring him . Lilith cackles and states that at the hour of spoken words , they will be freed and the Earth will perish . Meanwhile , the TARDIS has just landed nearby . Martha Jones steps outside and is amazed by the fact they ' ve gone back in time . Martha questions " when " they are , but the Doctor quickly pulls Martha back , keeping her from being struck by the contents of a chamber pot . The Doctor tells her it ' s " before the invention of the toilet " , apologising . However , Martha takes the event in stride as she has seen worse things in hospital . She then questions whether it is safe to walk around in the past , citing familiar time travel paradox es such as the grandfather paradox and the so - called " butterfly effect " . She frets over her reception as a black woman in a time when slavery still exists . The Doctor points out that he ' s not even human and to walk around like " you own the place " , just as he does . They walk around the town and the Doctor says Elizabethan England is far more like the 21st century than she might think . He points out there are things similar to the future : recycling , water cooler moments and people thinking the Earth will burn in flames AKA global warming . The Doctor deduce s both their location and the year : near the Globe Theatre in 1599 . He offers to take her to the theatre , to which Martha happily agrees to . The Doctor tells Martha when she gets home , she can tell everyone she ' s seen Shakespeare . However , Martha gleefully and sarcastically retorts that she would get sectioned . At the Globe , " Love ' s Labour ' s Lost " is on . Martha tells the Doctor how much she loved the play before pointing out that the male actors are dressed as women . The Doctor jokes , " London never changes " . Wanting to see the author himself , Martha starts what the Doctor thinks might be the first crowd chant for Shakespeare . The author himself comes on stage with the crowd cheering ; he ' s quite a bit different from his portraits : he ' s not bald or wearing a collar . The Doctor goes on about how much he admires Shakespeare ' s genius . However , the Doctor turns out to be wrong about the consistency of the Bard ' s genius when he asks the audience to shut their " big fat mouths " . Frowning , the Doctor moans in disappointment . Martha tells the Doctor he shouldn ' t meet his heroes . Shakespeare then announces there will soon be a sequel , " Love ' s Labour ' s Won " . Watching from above is Lilith , dressed as royalty . When Will is about to announce when the play will be performed , she takes control of his mind with a puppet ; Will declares that it will be tomorrow night . The Doctor is left bewildered by Will ' s sudden behaviour . Martha asks why she has never heard of " Love ' s Labour ' s Won " . The Doctor knows of the lost play as it appears in the listing of Shakespeare ' s works , but the play itself is non - existent . He decides to find out more about why it was never published . At The Elephant , Will and his actors are given beer by Dolly Bailey , the landlady and Will ' s lover . In the room is Lilith , disguised as a maid . The actors asks Will why he announced the play for tomorrow instead of next week as they planned . He states that he will have the last scene finished by the morning . The Doctor enters and Shakespeare tells him to leave . He says he won ' t give him an autograph or a portrait done with him . He adds that the Doctor not ask him where he gets his ideas . Upon seeing Martha enter , he stops dead . Recognising the signs , the actors excuse themselves ; to them , it looks like Shakespeare has found a new muse . Shakespeare is confused by Martha ' s clothing and the Doctor explains she ' s from " Freedonia " . Upon trying to pass himself off as " Sir Doctor of TARDIS " via the psychic paper , the Doctor is shocked to find that Shakespeare sees it as blank . Martha is confused by this as she sees the Doctor ' s title on the paper . Shakespeare remains adamant about what he sees and the Doctor explains the psychic paper , noting that Shakespeare ' s immunity to the paper proves the writer is an " absolute genius " . The writer takes interest in the word and wonders who the Doctor is . However , his attention shifts to Martha , whom he tries wooing , describing her as " a queen of Afric " or a " blackamoor lady " , which she finds slightly offensive . The Doctor says it ' s " political correctness gone mad " . At that moment , Lynley , Master of the Revels , barges in , demanding to see the script before he allows the play to proceed . Shakespeare tells him that the play will be given to him tomorrow morning ; however , Lynley arrogantly declares that the Master of Revels does not work to an author ' s schedule . He again demands the play , but Shakespeare insists it ' s not ready yet . Insulted , Lynley declares that this slight means he will ensure the play will never be performed , even if it ' s the last thing he does . The Doctor assumes that this explains why " Love ' s Labour ' s Won " was never shown . Martha , on the other hand , thinks it ' s karma for Will insulting her . Lilith overhears this , contacting her mothers to warn them ; they tell her that the play must be performed the next night . She tells them to calm down and chant with her , adding hair she secretly took from Lynley to a doll ; it is now a voodoo doll . Lillith plunges the doll into a bucket of water . The Doctor , Martha and Shakespeare hear a commotion in the street and run out , where Lynley vomits water . Lilith stabs the doll in the chest , and Lynley collapses , dead . Martha and the Doctor try helping him , with the Doctor noting that it ' s like something stuck Lynley ' s heart . Martha attempts CPR , but is shocked to find Lynely ' s lungs full of water . The Doctor calmly announces to the crowd that Lynley died a natural death , of an sudden imbalance of the humours . Confused , Martha asks the Doctor why he told the crowd a lie . The Doctor whispers that they ' ve got " one foot in the Dark Ages " , and any seemingly unnatural answer would lead them to think that it was witchcraft . When Martha asks what actually killed Lynley , the Doctor responds , " witchcraft " , confusing her further . Inside the inn , they wonder about Lynley ' s murder , but Shakespeare is equally confused by Martha ' s training as a doctor , wondering what kind of land Freedonia is . Martha defends herself by saying that in Freedonia , women can have any profession they want . He then asks the Doctor how he can have eyes so old for someone young . The Doctor says it because he reads a lot . Shakespeare sees it ' s a trite reply , something he ' d do himself . He then notes Martha looks at the Doctor like she ' s surprised that he even exists . The Doctor and Martha have been informed by Dolly that she ' s prepared a room for them . Shakespeare explains he still has to finish writing the end of the play and bids the Doctor good - night , saying he will solve why the constant performance from him tomorrow . The Doctor then gives Shakespeare his " All the world ' s a stage " line before retiring for the night . Martha is less than impressed with the room , complaining she doesn ' t even have a toothbrush . The Doctor gives her one from one of his pockets , explaining it contains Venusian spearmint . An excited Martha notes these odd events remind her of " Harry Potter " ; with a smile , the Doctor tells her " Wait to till you read book seven . Oh , I cried . " She then asks if magic is real , to which he states it doesn ' t — this looks like magic but isn ' t . She complains that she just started believing in time travel . The Doctor gives a disgruntled Martha mixed signals by casually sharing the bed with her only to show no interest , then dismissing the idea that a mere human could be channeling the psychic energy and bemoaning the lack of Rose ' s insight . However , without seeming to notice Martha ' s reaction , he attributes this to Martha being a novice to time travel . He says he ' ll take her back home tomorrow . Meanwhile , Lilith entrances Shakespeare and , using a marionette , compels him to write a strange concluding paragraph to " Love ' s Labour ' s Won " . She is discovered by Dolly , whom had just finished her work to " relax " Will . Lilith shows her true face , scaring Dolly with a snarl as she reaches to steal her broom . Upon hearing another scream , the Doctor and Martha run into to find Dolly ' s body as Shakespeare wakes . Through the window , Martha sees Lilith flying away on a broomstick . When asked by the Doctor what she saw , Martha answers , " A witch " . In the morning the Doctor , Martha and Shakespeare are confused by what has happened . Correctly guessing that Shakespeare is central to the witch ' s plot , Martha accidentally tells Shakespeare he will write about witches . Shakespeare then remembers Peter Streete spoke of witches ; he was the architect for the Globe Theatre . This leads the Doctor to investigate the Globe next . There , he wonders why the theatre is tetradecagal . The Doctor thinks he ' s heard of something before that involves the number 14 , but can ' t seem to remember it . Upon his companion s ' opinions of what has the number 14 in it , the Doctor asks why the Globe was designed like this . Shakespeare explains the architect thought it allowed the sound to carry well . When questioned as to the whereabouts of Peter Streete , Shakespeare says that he was admitted to Bedlam . The Doctor decides that is their next stop . Shakespeare follows after him after giving his actors the final draft of his play . He tells them the Queen may come , before muttering " As if . " The actors rehearse , with the lead actor reading what he thinks is gibberish ; he guesses Will was dozing off as he wrote it . This alerts the witches , who say it ' s too soon for their spell . However , Lilith tells them it ' s just a preview of what ' s to come that night . They all cackle in glee as a spirit appears to the actors before they can finish reading the spell . It fades away , making them decide to keep it a secret . Or else they risk getting committed to Bedlam . Once at Bedlam , Martha and the Doctor are disgusted to learn that the patients are whipped to entertain the gentry . Shakespeare defends it , saying that fear of the place helped " set him right " . The Doctor explains that Shakespeare fell into depression after his son ' s death . Shakespeare then speaks , " To be or not to be " , from his future play when explaining what he felt then , but wonders if the line is a bit pretentious ; the Doctor is indifferent about it . They are led into Streete ' s cell , where the Doctor finds he is suffering from catatonia . This visits causes Lillith to sense something is amiss ; she and her mothers look into their cauldron and find the Doctor at the mad house with Shakespeare . Lillith notes the Doctor was at the inn with Shakespeare , and smells of something new . Fearing that they be revealed if the Doctor can get Peter to talk , Lilith has Doomfinger transport herself . In the cell , the Doctor uses his telepathy to help Peter overcome his condition long enough to help explain what he went through . On the Doctor ' s order , Streete reveals that witches spoke to him and made him design the Globe to " their " design , not his own . He also tells the Doctor that the witches were based in All Hallows Street . Immediately , Mother Doomfinger appears in the cell and kills Peter with a touch . Martha yells to be let out , but is told by the Doctor that it ' s pointless as the entire building is yelling that . Doomfinger tells them she ' ll stop their hearts , asking who would like to go first . The Doctor steps forward to confront her ; Doomfinger explains no - one on Earth has knowledge of them . " Good thing I ' m here " the Doctor quips . He then begins rambling about the facts : humanoid females that channel energy into power through words . The Doctor figures out that the 14 walls of the Globe are based on the 14 stars of the Rexel configuration . He then names Doomfinger a Carrionite , which causes her to disappear . The Doctor explains the Carrionites produce their " magic " through an ancient science based on the power of words . In the witch ' s home , a wheezing Doomfinger rejoins Bloodtide and Lillith ; she tells them that the Doctor knows of their true nature . Lillith promises to kill the Doctor as the bells ring outside . She tells her mothers to go the Globe and wait for her ; Lillith will be waiting for the Doctor to find their home so she can kill him to put and end to his threat to their plans . Back at the Elephant , the Doctor explains that Carrionites vanished at the dawn of the universe ; thus it was left to debate whether they were actually real . However , its seems some of them are back . " How " they managed to get back is quickly figured out by Martha and the Doctor ; when Shakespeare was grieving for his son , he wrote something in a play that allowed them access to Earth . The Carrionites now plan on using Will ' s brilliant words to bring the rest of their species back ; " " Love ' s Labour ' s Won " is a weapon ! " The Doctor tells Shakespeare to stop the show whilst he and Martha go to All Hallows Street to thwart the witches . Per his orders , Shakespeare bursts on to the Globe ' s stage ; he tells the crowd that they will be getting a refund , but the play must not be performed . However , Bloodtide and Doomfinger are there waiting , using their voodoo doll to knock Will out by tapping its head . The actors think Will is drunk , and carrying him backstage . They apologise to the crowd , resuming the play . Amused , Bloodtide tells her sister that no - one that can stop them now ; humanity will doom itself . The Doctor and Martha reach All Hallows Street , with Martha questioning how this could cause a problem , as she ' s living proof that the world didn ' t end this year . Annoyed , the Doctor tells her that it ' s like " Back to the Future " , where if the past is significantly changed , the entire present day will be rewritten into something entirely different ; now fearful , Martha wonders if she ' s going to start fading away , to which the Doctor confirms if they don ' t stop the witches . The Doctor wonders which house they need to go to , finding a door opening itself ; " Make that witch house . " They confront Lilith , who is expecting them . She confirms the Doctor ' s suspicions : the three Carrionites hope to gain entry for the rest of their species , eliminate the humans , begin a new empire on Earth and spread out from there . Martha , mimicking the Doctor ' s actions at Bedlam , tries to neutralise her by speaking the name Carrionite , but Lilith mocks her , since naming only works once . Instead , she names Martha Jones , rendering her unconscious , muttering that she was unable to harm her more , as she must be out of her own time . Lilith tries to do the same to the Doctor , but fails , as her psychic power is unable to uncover his real name . She senses a name that could hurt him and tries to weaken him by naming " Rose " , but he assures her that that name keeps him fighting and demands to know how the Carrionites came to be on Earth . Lilith explains the Eternal s found the correct word to banish the Carrionites into darkness , but the three were able to escape using the power of Shakespeare ' s grief over his son — the grief of a genius — and intend to free the others . She approaches seductively , which the Doctor says definitely won ' t work on him , and then quickly cuts a lock of his hair . Taking flight through the window , she attaches the hair to a doll — which the Doctor says is basically a DNA replication module — and stabs it in the heart . The Doctor collapses , making Lilith think he ' s dead . She flies to the Globe , leaving them behind . Martha awakens , thinking the Doctor is dead as well , but finds he ' s still alive — two hearts . With her help , the Doctor manages to re - start his other heart . They proceed to the Globe to stop the Carrionites . However , at the Globe , the actors have already spoken the last lines of the play . Much to the horror of the audience , Carrionites emerge from a crystal held by the three and swarm outside . The Doctor and Martha arrive to find Shakespeare regaining consciousness and rubbing his head in pain . After making a joke about his eventual balding , the Doctor goes on stage to try undoing the damage , but finds only William can . Joining the Doctor , Shakespeare is told to improvise a verse to get rid of the Carrionites , as he is the most brilliant wordsmith . The Carrionites in the theatre wither in fear of his words , but William gets stuck on the last one , unable to think of a rhyme . Martha comes up with " Expelliarmus " ( a magic word coined by author J . K . Rowling in her Harry Potter books ) and the Carrionites — together with all the extant copies of " Love ' s Labour ' s Won " — are sucked through the portal . Martha , Shakespeare and the actors are left to take the applause of the audience who thought it was special effects . The Doctor finds the three " witches " trapped , screaming in their own crystal ball . In the morning , Shakespeare flirts once more with Martha . The Doctor walks out of the prop room , carrying a skull , which he states reminds him of a Sycorax ; Shakespeare states he ' ll take that word from him as well . " I must be on 10 % now " the Doctor thinks . The Doctor gives Will a neck brace for his pain , telling him to keep it as it looks good on him . Looking at the crystal ball , the Doctor says he has a nice dark attic for the Carrionites to scream in for all eternity , and that he needs to get Martha back to Freedonia . However , Will then tells the Doctor that he actually means they will be leaving this era . He reveals his deduction that the Doctor is not of the Earth and that Martha is from the future . Shakespeare tells the Doctor that they aren ' t so different ; both are men of great intellect . He ponders rewriting the play , but is told not to as it would risk releasing the Carrionites ( as they could manipulate him into rewriting the spell ) . Accepting this , Will announces that he will now focus on writing about sons and father in honour of his son , Hamnet . Martha is surprised by this , asking Will about the name ; obviously , Will is going to base " Hamlet " on his son . For his " Dark Lady " , he produces the sonnet , " Shall I compare thee to a summer ' s day ? " in her honour , but is interrupted when two of his actors burst in To Will ' s shock , they tell him the Queen wants to see the play , due to the audience thinking that the Carrionites were all part of the play ; however , this is not going to be possible as it was a one - off event . Queen Elizabeth enters , much to the Doctor ' s amusement . However , it seems they have met before in her past ; he is deemed her " sworn enemy " . The Queen declares , " Off with his head ! " This shocks the Doctor as he has yet to meet her , but comments that he is looking forward to finding out what he will do to offend her . He is then forced to run by Martha to keep himself alive . They run through the streets back to the TARDIS as the guards run after them . They enter the TARDIS , slamming the door just as an arrow embeds itself in the TARDIS ' exterior before dematerialisation .