We prove the main conjectures of [Bre12] (including a generalisation from the principal series to the cuspidal case) and [Dem], subject to a mild global hypothesis that we make in order to apply certain R = T theorems. More precisely, we prove a multiplicity one result for the mod p cohomology of a Shimura curve at Iwahori level, and we show that certain apparently globally defined lattices in the cohomology of Shimura curves are determined by the corresponding local p-adic Galois representations. We also indicate a new proof of the Buzzard-Diamond-Jarvis conjecture in generic cases. Our main tools are the geometric Breuil-Mézard philosophy developed in [EG14], and a new and more functorial perspective on the Taylor-Wiles-Kisin patching method. Along the way, we determine the tamely potentially Barsotti-Tate deformation rings of generic two-dimensional mod p representations, generalising a result of [BM12] in the principal series case.