optimized the code a little