Cleaned up functions and error reporting