Fixed some error function issues that could lead to unpredictable results if an error is caught by the end function