Root function working inconsistently