Refactor request generation and clean up code