Moved and modified from coinpunk