Moved code around to unify the approach