Massive code refactor, optimization, error handling, oh my!