This works but not as well as I'd like it to

There must be a better way.