Convert case to if, fix logic