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