Problem 12: refactor, same algorithm