Migrated most of masking code into output routine and fixed bugs with HBA handling