Cleanup loop.c

Cleanups loop.c to not use PATH_MAX stuff and duplicate resource cleanup handling which makes the flow hard to follow.

This is not tested as I have no dbp files. I suggest testing before merging.