completed code with final comments