added credit to Franck for his clean helper function code