Refactored by a js expert.