Fixed some bugs with NullPointerException