Refactored and added answer