Added a lot of functions for program interface. Fixed bugs. Code is optimized.