Code to handle orders and refactoring for ease of reading