Simplified and documented the code, and removed the Lodash dependency