Federer escapes upset, Dimitrov also through