optimize code, ehy so complicated?