updated final version with comments