torrent: code organization and simplification. Also cleanup up some memory leaks due to open/close.