Error Handling an code cleanup