Port more code from gist