Refactor heap integrity check into a function