Improved solution for Q4