Working code and comments added