Code cleanup and exception handling (maybe).