Dugald Drummond undertook the first trials with a powerful 4-4-0 express locomotive of the North British Railway in the 1870s. These twelve examples were from Neilson & Co. and from North British's own workshops and were delivered from 1877 onwards. They were also known as the Abbotsford class after one of the examples, or the Waverley class, named after the Edinburgh train station from which they departed.
From 1884, a total of 48 similar locomotives were built in several batches, which were designed by William P. Reid. The cylinders and the coupled wheels had the same dimensions, but the boiler was larger and worked at a higher pressure. In 1902 and 1904, the older types were also rebuilt to resemble the newer locomotives.
With the introduction of the new class scheme at North British in 1913, all were assigned to Class M, which also included other locos. Five machines of the first type were retired before 1923, all others came with the London & North Eastern group. Due to their different origins, the LNER classified the locomotives in different classes. Engines built from 1884 became the D31, those converted in 1902 became the D27 and those converted in 1904 became the D28. While the converted ones were retired by 1926, the last example of the D31 even survived the founding of British Railways and was in service until 1953.