Major cleanup, bugfixes and working json cookie (not used)