bug and performance fixes