(add_module): Complete rewrite. Use cleverer data structures and