added cookies, improved code