working version with comments added