Fix bugs related to wrong exception handling