Solution - with comments