modified code (refactored)