massive cleanup to remove duplicated code