Move take part of solution into seperate function to clean up code