Improve the ToObjectId function