Partial in the sense that it doesn't actually work for my input, only for the samples.
Also reformat the code, but that is nothing significant.