Reverting change introduced in 6facb575c which caused some logic bugs