major cleanup to make this more reusable