Restructure error messages, error classes, fix type errors