Fixed Resource Leaks, null dereferences, invalid value comparisons, invalid result set loop

Signed-off-by: Daan Hoogland <daan@onecht.net>