added cookie support
fixed repeated queries
reverted accidental change
Cookie support from Matej, with minor tweaks