refactor code for popup page