Changed code to use wpdb prepare in favour of the deprecated wpdb escape function. Added some isset checks to avoid unset GET warnings.