Copy of code from edx-platform