added original codes and adjusted a bit for compilation.