Molecular mechanisms of crystallization and defect formation