Edited the Code to catch invalid Hierarchied