optimised collision code for example 1