Add more code from miloops fork