 	(buffer (current-buffer)))
     (let* ((n (buffer-name buffer)) (n1 (substring n 0 1)))
       (if (and (not (or (string= " " n1) (string= "*" n1))) (buffer-modified-p buffer))
-	  (message-box "The current buffer has been changed")
+	  (message-box "The current buffer has been changed, please save it first.")
 	(erl-spawn
 	  (erl-send-rpc wrangler-erl-node 'wrangler_distel 'rename_var (list current-file-name line-no column-no name wrangler-search-paths tab-width))
-	  (erl-receive (buffer line-no column-no)
+	  (erl-receive (line-no column-no current-file-name)
 	      ((['rex ['badrpc rsn]]
 		(message "Refactoring failed: %S" rsn))
 	       (['rex ['error rsn]]
 		(message "Refactoring failed: %s" rsn))
-	       (['rex ['ok refac-rename]]
-		(with-current-buffer buffer (progn (revert-buffer nil t t)
-						   (goto-line line-no)
-						   (goto-column column-no)))
-						  ;; (goto-char (get-position line-no column-no))))
-		(message "Refactoring succeeded!")))))))))
+	       (['rex ['ok modified]]
+		(progn
+		  (if (equal modified nil)
+		      (message "Refactoring finished, and no file has been changed.")
+		    (if (yes-or-no-p "Do you want to preview the changes to be performed?")
+			(ediff current-file-name (concat current-file-name ".swp"))
+		      (commit-or-abort)))
+		  (with-current-buffer (get-file-buffer current-file-name)
+		    (goto-line line-no)
+		    (goto-column column-no))))
+		)))))))
+		  
 
 (defun erl-refactor-rename-fun (name)
   "Rename an identified function name."

  "Search for large modules"
  (interactive (list (read-string "Number of lines: ")))
  (let 	(buffer (current-buffer))
    (if (buffer-modified-p buffer) (message-box "Buffer has been changed")
      (erl-spawn
	(erl-send-rpc wrangler-erl-node 'wrangler_code_inspector 'large_modules(list lines wrangler-search-paths tab-width))
	(erl-receive (buffer)

				(erl-spawn
				  (erl-send-rpc wrangler-erl-node 'refac_register_pid 'register_pid_2
						(list current-file-name start-line-no start-col-no end-line-no (- end-col-no 1) name wrangler-search-paths tab-width))
				  (erl-receive (current-file-name start-line-no start-col-no)
				      ((['rex ['badrpc rsn]]
					(message "Refactoring failed: %S" rsn))
				       (['rex ['error rsn]]
					(message "Refactoring failed: %s" rsn))
				       (['rex ['ok modified]]
					(progn
					  (if (yes-or-no-p "Do you want to preview the changes to be performd?")
					      (progn
						(setq modified-files (cdr modified))
						(ediff current-file-name (concat current-file-name ".swp")))
					    (commit-or-abort))
					  (with-current-buffer (get-file-buffer current-file-name)
					    (goto-line start-line-no)
					    (goto-column start-column-no)))
					))))
			      (message "Refactoring aborted!")))
			   (['rex ['ok modified]]
			    (progn
			      (if (yes-or-no-p "Do you want to preview the changes to be performd?")
				  (progn
				    (setq modified-files (cdr modified))
				    (ediff current-file-name (concat current-file-name ".swp")))
				(commit-or-abort))
			      (with-current-buffer (get-file-buffer current-file-name)
				(goto-line start-line-no)
				(goto-column start-column-no)))
			    ))))
		  (message "Refactoring aborted!")))
	       (['rex ['unknown_pids pars]]
		(if (yes-or-no-p "Do you want to continue the refactoring?")
		    (erl-spawn
		      (erl-send-rpc wrangler-erl-node 'refac_register_pid 'register_pid_2
				    (list current-file-name start-line-no start-col-no end-line-no (- end-col-no 1) name wrangler-search-paths tab-width))
		      (erl-receive (currnet-file-name start-line-no start-col-no)
			  ((['rex ['badrpc rsn]]
			    (message "Refactoring failed: %S" rsn))
			   (['rex ['error rsn]]
			    (message "Refactoring failed: %s" rsn))
			   (['rex ['ok modified]]
			    (progn
			      (if (yes-or-no-p "Do you want to preview the changes to be performd?")
				  (progn
				    (setq modified-files (cdr modified))
				    (ediff current-file-name (concat current-file-name ".swp")))
				(commit-or-abort))
			      (with-current-buffer (get-file-buffer current-file-name)
				(goto-line start-line-no)
				(goto-column start-column-no)))
			    ))))
		  (message "Refactoring aborted!")))
	       (['rex ['ok modified]]
		 (progn
		   (if (yes-or-no-p "Do you want to preview the changes to be performd?")
		       (progn
			 (setq modified-files (cdr modified))
			 (ediff current-file-name (concat current-file-name ".swp")))
		     (commit-or-abort))
		   (with-current-buffer (get-file-buffer current-file-name)
		     (goto-line start-line-no)
		     (goto-column start-column-no))))
		 )))))))

(defun erl-refactor-move-fun (name)
  "Move a function definition from one module to another."
  (interactive (list (read-string "Target Module name: ")
		     ))
  (let ((current-file-name (buffer-file-name))
	(line-no           (current-line-no))
