Proper conn closing and settings refactoring