Cleaned up and fixed potential null pointers