Added some custom patterns and tidied up this ugly code a bit.