Complete code with all commments