Some improvements based on Turini's suggestions.