solution and my work
added refactored solution