Improvements based on Lucas's suggestions.