faster and simpler version