fixed bugs according to the suggestions given by jwl