Cleaning up modreg.c

Moving stuff to tree_utils.c