Fixed missing cleanup when an exception is thrown