Add some sleeping to the killing of emacs to give it time to shutdown. Also remove the desktop lock file if present after stopping.