Added check for closed connection