refactor and organize code