Spent almost all day to figure this out