Fix bug, write common functions