remporarily added lots of debug messages for further improvement, added log_debug method for controllable debugging