Fixed an issue with an unclosed query in the lottery algorithm