refactored improved code