Optimal capital structure and endogenous default