Loop expansion around the Bethe approximation through the M-layer construction