Refactored javascript code into self-executing anonymous function