changed writeToSDM and working on permutation for better code