Kernel rewritten

Performed all kernel rewrite, in order to improve the efficiency, it is
not tested already.