Fixed problem with max_id calculation