Rewritten entire code JS