clean exit on interrupt, minor query optimozations