Fixed error when constructing when clause