code clone from https://code.google.com/p/pinta/