Added try-catch to check for missing namespace definitions, tidied code.
Tidied code.