adding recursive check and fixing return behaviour