made function handling more robust