Added checks to prevent NullPointerException.