Spin decoherence in inhomogeneous media