Made functions and querys more robust