Clean up and refactored code

Refactored some duplicated code, beefed up the handling of invalid ID’s
entered by the user, and did some other general cleanup and readability
improvements.