Compiler refactoring and bug fixing