refactor, reorganize code