Fix two (somewhat related) issues with masked tables.

An alternative implementation to fix #916