optimize code logic