Cleaned up finding of free lock instance

The previous algorithm had a O(n**2) list creation and O(n**2) search
for the lowest not-taken lock instance. One could argue that
the numbers involved are small enough to not make a difference, but
this version has a O(n logn) list creation and O(n) search without being
more complicated, so it might be worth it.