Working version with comments