Removes unnecessary intermediate tables