Reorganize some js code