Final logic code