Cleaning up some of the logic