When you want to replace tab with spaces or vice versa don't use
M-x tabify or
M-x untabify. They work on the
current selection so if you want it to be applied to a whole buffer,
C-x h (mark-whole-buffer) before to select the whole buffer.