working verison in C. Break DRY