Removed superfluous code, redid the input format to make the algorithm simpler.