Reduce and optimize code