Fixed multiple potential performance issues