Code cleanup. Nicer exception handling.