Bug Fix: program will endlessly repeat executing when lines left less than 10 and do some refining to the code