refactor modal code