more robust logic for case when lcm does not exist