Halide-driven formation of lead halide perovskites: insight from ab initio molecular dynamics simulations