Refactored to produce less js code