reordered logic blocks so that it actualy has right logic