Updated with changes from @garrettr