Added template masking stage

Please review and let me know if I should make any changes.