Add code from miloops fork