improved elements overflow checking. cleaned code