Added comments, improving algorithm