Adapted code slightly for emacs 24.4.