Port from cloudpebble.net