added a ton of comments and code optimization.