rewritten, errors fixed