fix: Update exception handling and improve code structure