added double buffering, cleaned up other code