This func is huge, at 250 lines long.