Re-wrote main method, fixed algorithm