Bugfix and improvements on mask_shallow().