Fixed most of the issues in performance which were the result of using linked
lists badly.