merged some ideas from https://github.com/hammacher fork