Fixed problem with connections not being closed, but still to be organized better.