Optimize and fix tests