Fixing more memory leaks and moving domain storage stuff into functions.