getting closer with alg. works for some cases