Also fixed a lot of bugs related to materials Former-commit-id: 54086eb3aa36c1a7b31b55056967275e6c24a4c6