Fix type error when values are null (weird)

+ close connection when required