fixed bugs in above code