Partial refactoring.
Fixed conditions.