refactored to get easy to understand