Finished JavaScript refatoration