made modifications mostly for error handling