Use setcookie instead of assigning variable to $_COOKIES fixes issue #1.
Plus small refactoring and formatting.