Perturbation theory and free resolutions for nilpotent groups of class 2