optimize code and fix entry bugs