added some pointer checks and debug information