Make collision checking more generic, split large function