refactored latest attempt