To be debugged and refactored.