Slightly different implementation of hxim's idea. Take 3 (be even more cautious with weak pawns in midgame).