code cleaning + error handling