optimized some of the code