Cleaned up code and added types for kernel and task state