Reorganize some code