final version, fixed bugs