Fixing a bug and tweaking code