Move consecutive collision algorithm to a function