add debug messages and fix some warnings
improve error handling of wait_for_event