Added more exception catching, introduced error codes.