code fully refactored