Updated code to work with back/forward buttons