modularized version
fix some errors