Fixed bug found by Ehsan Naeini