Refactored and made much simpler. No more need for nginx restart