move code around to consolidate checks in one place