implement look ahead

modularize the code, and implement one step look ahead
optimized code

optimized many parts of the code to run faster