Fix moc gen

Reverts previous fix and then reapplies it with other pull requests under it. (more complete fix to regression of #5)