Added multiple relation support and some refactoring