optimize work flow to reduce database operation

optimize work flow to reduce database operation, also refactor some
methods