refactoring
more refactoring
adding thread API