Improved solution 12 and separated common reusable code.