corner cases handled & some minor changes in gates.py

There were some corner cases that were not handled properly. 
Indentation also changed.