Added common functions, fixed bugs