Fixed null dereference crash, and cleaned up