Fixes some null pointer cases and adds comments