try to fix connection leak, remove dup code