added capture watchdog function,
code reorganization and bug fix