add debug output
fix exception handling and checks