More modularization and fix some function names