add exception handler, add licence header, fix bugs which blocked some long running tests (thread hanging issues). Optimize a bit.