Small changes, added better error handling.