refactor some of the kernel code