refactored compile logic into more modular functions