Simplified conditions and removed duplicated code.