refactored for more clear structure