Fixed the heap variable problem. Except minor bug fixes, this should be
the final version.