Fix several errors and improve behaviour when loading dynamic libraries.