Small improvements based on @joaocarlos work