fix segfault due to not properly loading and unloading files