rearrange javascript in order to clean code