added documentation and more logic for checking