Remove 'switch'/'given'/'when', improve error-handling, and general code clean-up