Remove function copied from stackoverflow and fix bug.