Working code with comments