re-factory some code