refactoring c version