Moved pushing to history to a separate function