Remove all instances of die();  and replace them with goto end;