Added some checks to avoid NPE.
Merge remote-tracking branch 'upstream/master'