use a more cleaned way to query and return error code