Change (end-of-buffer) to (goto-char (point-max))

(end-of-buffer) is only for interactive use.