Not the easiest to figure out but got there in the end