fix naming issue and optimize code