fix gate delay bug and refactoring