Refactor heap sort