merged code from SkPhilipp