Fixed a few bugs in masks generation.