Fixing an occasionaly null pointer exception