added more logical checking