fixed some ugly logic