In May 1747, Bach visited the court of King Frederick II of Prussia in Potsdam. The king played a theme for Bach and challenged him to improvise a fugue based on his theme. Bach obliged, playing a three-part fugue on one of Frederick's fortepianos, which was a new type of instrument at the time. Upon his return to Leipzig he composed a set of fugues and canons, and a trio sonata, based on the Thema Regium (theme of the king).