working code with one bug. JS comments added