fix based on https://gist.github.com/timbrom/1942280