Added type checks for more reliability