refactor functions, refine style