Factor out setTimeout, fix type error