Code review comments.

  - removed needless nested function
  - fixed potential bug with mutable value of keyword argument.
  - fixed incorrect usage of query variable.