refactoring & closing connection