Python code fixed, query handler changed