Optimized the tool code a bit