Took from github with some modification