Global refactoring + small corrections
Separate functions for type and maskP parameters