added menu prompt and improved the algorithm

Still do not get correct results yet but added easier to read logics and
comments for debugging.