Added extra checks to improve performance