Modularize and add exception handling.