Sorry about the first comment, I meant to say ksAlgorithm is computed the same way in both if statements, so that computation could be moved to the top.