P027: greatly improved solution