fixed bugs found by pavel kovalev