fixing code to optimize