refactored completely