Improved code to check if root