ok giant function but hopefully it is worth it