Functional version of code