Added getElem optimization
Improved code format