Final code with comments.