Optimized with Java 7, modified to use double buffering