In regards to your (for lack of a better term, DLL unload code), remember that $dll(0) will be updated each time a dll is unloaded, so I would recommend using
while $dll(0) { .dll -u $dll(1) }
This will unload the DLL's in the same order that they were loaded.