Code style cleanup and some error handling enhancements