Added After kernel code, changed code location

I took items out of the while loop where needed, and added the code that
needs to run after the kernel executes.