complete code with comments