Dramatically reduced energy impact by improving code efficiency

Cached various information to reduce calls to get information from
hardware. Avoided dynamic memory allocation. Added brief comments and
TODOs.