kernel: fix some leaks and clean up