More code to compile gates. Fixed some bugs