refactor and clean up beginning