updated with comments and minor changes