rewrite heapfy function