Fix bugs in worksheet code