A bit of re-structuring. Move module lookup into layout class.

This also fixes a bug whereby a variable that was not in lexical
scope could be returned.
Minor fix to function layout descent.