Why does moving logic up work ,but down doesn't?

OK, not understanding why this is doing what it’s doing. I can select the number of moves for logic bricks and up works like it should ,but down makes it go all the way to bottom. Any ideas?

Here’s code:


# THIS SCRIPT IS LICENSED UNDER GPL,
# please read the license block.
#
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_info = {
    "name": "Move Logic",
    "author": "",
    "version": (1, 0),
    "blender": (2, 76, 0),
    "location": "Logic Editor > Properties > Move Logic Tab",
    "description": "Move logic by using logic brick names.",
    "warning": "",
    "wiki_url": "",
    "category": "Game Engine",
} 

 
import bpy
from bpy.props import StringProperty, BoolProperty, IntProperty


#Move Logic Panel        
class MoveLogicPanel(bpy.types.Panel):
    """Creates a Panel in the Logic Editor properties window"""    
    bl_label = "Move Logic"
    bl_options = {'DEFAULT_CLOSED'}    
    bl_idname = "OBJECT_move_logic"
    bl_space_type = 'LOGIC_EDITOR'
    bl_region_type = 'UI'
    

    def draw(self, context):
        layout = self.layout
        
        act_obj = context.active_object
        
           
        row = layout.row()
        row.prop(act_obj, "move_down")         
        row.operator("object.move_logic", text="Up / Down", icon='CONSTRAINT')
        row = layout.row()
        row.prop(act_obj, "number_moves")
        row = layout.row()         
        row.prop(act_obj, "sensor_move")
        row = layout.row()                
        row.prop(act_obj, "controller_move")
        row = layout.row() 
        row.prop(act_obj, "actuator_move")                        
        row = layout.row()                                                                         
#End of Move Logic Panel 
###################################################################################### 


######################################################################################
#Moving Logic Operator  
class MoveLogic(bpy.types.Operator):
    """Move logic"""        
    bl_idname = "object.move_logic"
    bl_label = "Move Logic"

    
    def execute(self, context):
        
        act_obj = bpy.context.active_object
        num_move = context.object.number_moves
        down = context.object.move_down
                                
        #Move Controller        
        for k in act_obj.game.controllers:
            ctrl = k.name
            ctrl_name = context.object.controller_move            
            #Up            
            if ctrl_name == ctrl and down == False:               
                def move_controller(num_logics):
                    for k in range(num_logics):                                       
                        bpy.ops.logic.controller_move(controller = ctrl, direction='UP')                        
                move_controller(num_move)
            #Down                
            if ctrl_name == ctrl and down == True:               
                def move_controller(num_logics):
                    for k in range(num_logics):                                       
                        bpy.ops.logic.controller_move(controller = ctrl, direction='DOWN')                        
                move_controller(num_move)                
                                                        
                                    
        #Move Sensor
        for i in act_obj.game.sensors:
            sens = i.name                    
            sens_name = context.object.sensor_move                                                       
            #Up
            if sens_name == sens and down == False:                        
                def move_sensor(num_logics):
                    for i in range(num_logics):                                       
                        bpy.ops.logic.sensor_move(sensor = sens, direction='UP')                        
                move_sensor(num_move)
            #Down
            if sens_name == sens and down == True:                        
                def move_sensor(num_logics):
                    for i in range(num_logics):                                       
                        bpy.ops.logic.sensor_move(sensor = sens, direction='DOWN')                        
                move_sensor(num_move)

                
        #Move Actuator
        for d in act_obj.game.actuators:
            act = d.name 
            act_name = context.object.actuator_move
            #Up                                 
            if act_name == act and down == False:                         
                def move_actuator(num_logics):
                    for d in range(num_logics):                                       
                        bpy.ops.logic.actuator_move(actuator = act, direction='UP')                        
                move_actuator(num_move)
            #Down                
            if act_name == act and down == True:                         
                def move_actuator(num_logics):
                    for d in range(num_logics):                                       
                        bpy.ops.logic.actuator_move(actuator = act, direction='DOWN')                        
                move_actuator(num_move)


                                                
        return {'FINISHED'}
######################################################################################        
 
        
def register():
    bpy.utils.register_class(MoveLogicPanel)
    bpy.utils.register_class(MoveLogic)   
    bpy.types.Object.sensor_move = bpy.props.StringProperty(
        name = "Sensor",
        description = "Name of sensor to move",
        default = ''
      )           
    bpy.types.Object.controller_move = bpy.props.StringProperty(
        name = "Controller",
        description = "Name of controller to move",
        default = ''
      )
    bpy.types.Object.actuator_move = bpy.props.StringProperty(
        name = "Actuator",
        description = "Name of actuator to move",
        default = ''
      )                         
    bpy.types.Object.move_down = bpy.props.BoolProperty(
        name = "Move Down",
        description = "Check to move logic down",
        default = False
      )
    bpy.types.Object.number_moves = bpy.props.IntProperty(
        name = "Number Moves",
        description = "Number of moves",
        min = 1,
        max = 40
      )         
    
def unregister():
    bpy.utils.unregister_class(MoveLogicPanel)
    bpy.utils.unregister_class(MoveLogic)
    del bpy.types.Object.sensor_move          
    del bpy.types.Object.controller_move
    del bpy.types.Object.actuator_move       
    del bpy.types.Object.move_down
    del bpy.types.Object.number_moves                        
    
if __name__ == "__main__":
    register()