added matrix building function