Refactors and start of handling close connection