I would guess that your boolean flags are not being set correctly. The code which you've shown doesn't look it would work:
If no flags are set unless the context position is over an object, how can it work where there is no object?
If no flags are set unless the context position is over an object, how can it work where there is no object?