merged some functions from yhidaka notebook