refactored solution 5 to run a little faster