Functional version with comments