Fix bad module naming when using custom output, again

This mostly reverts commit d0f4149c2a426ad136bab6559bc2dd1c712492b4
and fixes the issue described.