Fixed potential nullpointer exceptions