fixed faulty if logic