improving on the error handling code and some minor changes