Added comments and fixed bugs