This is super ugly. Need some better way to resolve the scoping issues here.