Fixed thread exception handling and some other minor issues