Wessex was invaded by the Danes in 871, and Alfred was compelled to pay them to leave. They returned in 876, but were forced to withdraw. In 878 they forced Alfred to flee to the Somerset Levels, but were eventually defeated at the Battle of Edington. During his reign Alfred issued a new law code, gathered scholars to his court and was able to devote funds to building ships, organising an army and establishing a system of burhs. Alfred's son, Edward, captured the eastern Midlands and East Anglia from the Danes and became ruler of Mercia in 918 upon the death of his sister, Æthelflæd. Edward's son, Æthelstan, conquered Northumbria in 927, and England became a unified kingdom for the first time. Cnut the Great, who conquered England in 1016, created the wealthy and powerful earldom of Wessex, but in 1066 Harold Godwinson reunited the earldom with the crown and Wessex ceased to exist.