added lots of comments for code clarity