Added counter resetting and thread interruption to fix #3