refactor main and modal code