With a brief view of your code (no time for more) i can thing about two things:

1) check your mouse_range maybe needs to be increased

2) where did you get the temp=x ? "x" its reserved for the engine, change it with other name, and i advice you to give meaning names to the variables and functions