Refactored using ekmett's heap library.