Refactored Heap code