code rewritten. now works with odd numbers of modules, too