some corner cases handled

In gates.py, some corner cases were not handled properly.
Indentation also changed.