added changes to make sure it handles situation where there is no solution