Added several NullPointer protections