reorganize some code