add main() and several changes in code