fixed some bug and tested the main program