working code, comments added