refactor javascript, no more spaghetti code.