Added comments and fixed a bug