[API] Improved error management and cleaned code