Closes #328 (Modify REST API to work in federated environments) and some refactorings