This is an adaption from Hatcher, but basically I am confused about the last step. After we conclude $f^{-1}(x) \subset \cup_{i < m} (a_i,b_i)$. How do we conclude $f(a_i), f(b_i)$ is on the boundary? And I am confused about creating this loop $g$ from the partition.
Also just curious, does the definition of a loop include "inner loops". Let $x_0$ be the based point. 

If $f(a_i), f(b_i)$ are not in the boundary of $B$, just shrink $B$.