Code tidy.
Updated to close & free the loop when done.