added middleware to enable CORS. other minor refactoring