added more comments and fixed bugs