cleaned up some messy logic