use memoisation to speed up queries