refactored implementation