Remove code duplication and split execute and query