Solving in a more functional style.