improve coding style
fix bugs to make it work