kheap: refactoring and code cleanup