refactored previous attempt