Some optimizations and code cleaning