Firts core functions
Fixed some mistakes in error handling