Refactored openModal function to manage input and output variables