cleaned up some of the logic