updated with comments in the solution