I am trying to read Nielsen's and Chuang's book on quantum computing and I am having problem understanding Deutsch's algorithm. 
According to my understanding of the algorithm, the state $|x\rangle$ between the Hadamard gates should not change, however somehow the state of the second qubit affects the first? So from my point of view the measurement after the second Hadamard gate should always be $|0\rangle$. So is $|x\rangle$ after the $U_f$ not the state we used as input? If not then what is the state? Every answer will be appreciated, thanks in advance.