Cannot get the logic right :(