Code review comments.

  - removed needless nested function
  - fixed potential bug with mutable value of keyword argument.
  - fixed incorrect usage of query variable.
Maybe it's time to change `q` for something more meaningful?