Updated query function / added type checks