Refactor kernel code into separate library

Made a separate library for the kernel code
which is common between applications.