added comments and additional null dereference check.