Rename some variables for clarity and fix exception handling