Code optimization

Updated certain code for efficiency