refactor schema, added tests and improve error handler