Fixed nullpointer and added some debug logging