Fixed problem with max_id in query