Solve Project 8 ugly function