Simplify the program a bit for some edge case handling but it is still too slow to pass the large data set.