For anyone wondering how it's done. Look at line 41+