code refactor and clean up