bugfixes

Cleaning up some code, some global decs moved to the start of
functions, one string localised, one sql function with hardcoded in
changed to get_in_or_equal