Add suport for env variable base_key + Major cleanup of server code