cleaned up the logic slightly to be more pretty