JavaScript more efficient and added comments