Added more fixes and tests to when()