Use a tryClose() method

Makes the code cleaner