Clean code and fix tests