improved solution for level 18