modified source code to handle special cases in rotation matrix