1. clean code and add some helper function