Support for caching and restoring masks. Fixes issue #24.