added solution with comments