Refactor the underlying structures in the kernel to be more efficient. Add extra sanity checks.