refactor recognize thread and add training thread
cleanup and lots of comments