More error handling and less hard-coded things