Speed improvement using a heap list instead of nested lists.